package com.softservecom.training.shop.dao;

import java.util.List;
import java.util.Set;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.LockMode;
import org.hibernate.Query;

import com.softservecom.training.shop.pojos.ProductsCategories;

import static org.hibernate.criterion.Example.create;

/**
 * A data access object (DAO) providing persistence and search support for
 * ProductsCategories entities. Transaction control of the save(), update() and
 * delete() operations can directly support Spring container-managed
 * transactions or they can be augmented to handle user-managed Spring
 * transactions. Each of these methods provides additional information for how
 * to configure it for the desired type of transaction control.
 * 
 * @see com.softservecom.training.shop.pojos.ProductsCategories
 * @author MyEclipse Persistence Tools
 */

public class ProductsCategoriesDAO extends BaseHibernateDAO {
	private static final Log log = LogFactory
			.getLog(ProductsCategoriesDAO.class);
	// property constants
	public static final String GROUP_NAME = "groupName";
	public static final String RAITING = "raiting";

	public void save(ProductsCategories transientInstance) {
		log.debug("saving ProductsCategories instance");
		try {
			getSession().save(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	public void delete(ProductsCategories persistentInstance) {
		log.debug("deleting ProductsCategories instance");
		try {
			getSession().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public ProductsCategories findById(java.lang.Integer id) {
		log.debug("getting ProductsCategories instance with id: " + id);
		try {
			ProductsCategories instance = (ProductsCategories) getSession()
					.get(
							"com.softservecom.training.shop.beans.ProductsCategories",
							id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public List<ProductsCategories> findByExample(ProductsCategories instance) {
		log.debug("finding ProductsCategories instance by example");
		try {
			List<ProductsCategories> results = (List<ProductsCategories>) getSession()
					.createCriteria(
							"com.softservecom.training.shop.beans.ProductsCategories")
					.add(create(instance)).list();
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	public List findByProperty(String propertyName, Object value) {
		log.debug("finding ProductsCategories instance with property: "
				+ propertyName + ", value: " + value);
		try {
			String queryString = "from ProductsCategories as model where model."
					+ propertyName + "= ?";
			Query queryObject = getSession().createQuery(queryString);
			queryObject.setParameter(0, value);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	public List<ProductsCategories> findByGroupName(Object groupName) {
		return findByProperty(GROUP_NAME, groupName);
	}

	public List<ProductsCategories> findByRaiting(Object raiting) {
		return findByProperty(RAITING, raiting);
	}

	public List findAll() {
		log.debug("finding all ProductsCategories instances");
		try {
			String queryString = "from ProductsCategories";
			Query queryObject = getSession().createQuery(queryString);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	public ProductsCategories merge(ProductsCategories detachedInstance) {
		log.debug("merging ProductsCategories instance");
		try {
			ProductsCategories result = (ProductsCategories) getSession()
					.merge(detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public void attachDirty(ProductsCategories instance) {
		log.debug("attaching dirty ProductsCategories instance");
		try {
			getSession().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(ProductsCategories instance) {
		log.debug("attaching clean ProductsCategories instance");
		try {
			getSession().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}
}